﻿#include <crtdbg.h>
#include "video.h"


#pragma comment (lib, "./sdl/lib/SDL.lib")

CVideo::CVideo()
{
	m_screen = NULL;
	m_overlay = NULL;
	m_yuv = NULL;
}

CVideo::~CVideo()
{
	SDL_FreeYUVSurface(m_yuv);
	SDL_Quit();
}

bool CVideo::InitVideo(int w, int h)
{
	if ( SDL_Init( SDL_INIT_VIDEO | SDL_INIT_TIMER ) < 0 ) 
		return false;

	m_video_width = w;
	m_video_height = h;

	const SDL_VideoInfo *info;
	info = SDL_GetVideoInfo();

	m_video_bpp = info->vfmt->BitsPerPixel;

	int video_flag = SDL_HWSURFACE|SDL_NOFRAME;
	if (( m_screen = SDL_SetVideoMode(m_video_width, m_video_height, m_video_bpp, video_flag)) == NULL )
	{
		return false;
	}

	// win32
	SDL_SysWMinfo i;
	SDL_VERSION( &i.version );
	if ( SDL_GetWMInfo ( &i) ) 
	{
		m_hwnd = i.window;

		RECT r;
		::GetWindowRect(m_hwnd, &r);  
		m_window_posx = r.left;
		m_window_posy = r.top;

		SDL_EventState(SDL_SYSWMEVENT, SDL_ENABLE);
		DragAcceptFiles(m_hwnd, TRUE);
	}

	return true;
}

#define OVERLAY_HEIGHT	32

bool	CVideo::InitOverlay(int width, int height, int format )
{
	unsigned int rmask, gmask, bmask, amask;
// 	rmask = 0x000000ff;
// 	gmask = 0x0000ff00;
// 	bmask = 0x00ff0000;
// 	amask = 0xff000000;

	rmask = 0xff000000;
	gmask = 0x00ff0000;
	bmask = 0x0000ff00;
	amask = 0x000000ff;

	_ASSERT(m_screen);
	m_overlay = SDL_CreateYUVOverlay(width, height, SDL_YV12_OVERLAY, m_screen);
	m_yuv = SDL_CreateYUVSurface(width, OVERLAY_HEIGHT);
	m_rgb = SDL_CreateRGBSurface(SDL_SWSURFACE, width, OVERLAY_HEIGHT, 32, rmask, gmask, bmask, amask);

	_ASSERT(m_screen);
	if( m_overlay == NULL ) 
		return false;

	return true;
}

void	CVideo::RenderOverlay( int width, int height )
{
	SDL_Rect rect;
	rect.x = 0;
	rect.y = 0;
	//rect.w = width;
	//rect.h = height;

	rect.w = m_video_width;
	rect.h = m_video_height;
	SDL_DisplayYUVOverlay(m_overlay, &rect);
}

void	CVideo::LockOverlay()
{
	SDL_LockYUVOverlay(m_overlay);
}

void	CVideo::UnLockOverlay()
{
	SDL_UnlockYUVOverlay(m_overlay);
}

/*
void OpenRemotePlay::UpdateOverlay(void)
{
	SDL_Color color;
	color.r = color.g = color.b = 255;
	SDL_Surface *surface = NULL;

	if (font_normal) 
	{
		surface = TTF_RenderUTF8_Blended(font_normal,
		os_caption.str().c_str(), color);
	}
	if (!surface) return;

	SDL_Rect rect;
	rect.x = rect.y = 0;
	rect.w = view.status_bg->w;
	rect.h = view.status_bg->h;
	SDL_FillRect(view.status_bg, &rect,	SDL_MapRGBA(surface->format, 0, 0, 0, 128));

	if (mode_game && !strncasecmp(exec_mode.c_str(), "gam", 3)) 
	{
		rect.w = mode_game->w;
		rect.h = mode_game->h;
		SDL_BlitSurface(mode_game, NULL, view.status_bg, &rect);
	}
	else if (mode_ps1 && !strncasecmp(exec_mode.c_str(), "ps1", 3)) 
	{
		rect.w = mode_ps1->w;
		rect.h = mode_ps1->h;
		SDL_BlitSurface(mode_ps1, NULL, view.status_bg, &rect);
	}
	else if (mode_vsh && !strncasecmp(exec_mode.c_str(), "vsh", 3)) 
	{
		rect.w = mode_vsh->w;
		rect.h = mode_vsh->h;
		SDL_BlitSurface(mode_vsh, NULL, view.status_bg, &rect);
	}

	rect.x = ORP_OVERLAY_HEIGHT + 2;
	rect.y = (ORP_OVERLAY_HEIGHT - surface->h) / 2;
	rect.w = surface->w;
	rect.h = surface->h;
	SDL_BlitSurface(surface, NULL, view.status_bg, &rect);

	#if 0
	surface = TTF_RenderUTF8_Blended(font_small, exec_mode.c_str(), color);
	if (!surface) return;

	rect.w = surface->w;
	rect.h = surface->h;
	rect.x = view.status_bg->w - surface->w;
	SDL_BlitSurface(surface, NULL, view.status_bg, &rect);

	#endif

	ConvertRGBtoYUV(view.status_bg, view.status_yuv);
	view.status_ticks = SDL_GetTicks() + 2000;
}

*/

void	CVideo::Render(int x, int y, SDL_Surface *surface)
{

	SDL_Rect rect;
	rect.x = rect.y = 0;
	rect.w = m_rgb->w;
	rect.h = m_rgb->h;
	//SDL_FillRect(m_rgb, &rect,	SDL_MapRGBA(surface->format, 0, 0, 0, 128));

// 	SDL_SetAlpha(m_rgb, SDL_SRCALPHA, 128);
// 	SDL_SetColorKey(m_rgb, SDL_SRCCOLORKEY, SDL_MapRGB(m_rgb->format, 0, 0, 0));
	SDL_FillRect(m_rgb, &rect,	SDL_MapRGBA(m_rgb->format, 0, 0, 0, 128));

	rect.x = OVERLAY_HEIGHT + 2;
	rect.y = (OVERLAY_HEIGHT - surface->h) / 2;
	rect.w = surface->w;
	rect.h = surface->h;
	SDL_BlitSurface(surface, NULL, m_rgb, &rect);

	ConvertRGBtoYUV(m_rgb, m_yuv);

	SDL_Rect src, dst;
	src.x = src.y = 0;
	src.w = m_rgb->w;
	src.h = m_rgb->h;

	dst.x = 0;
	dst.y = 100;
	//dst.y = rect.h - m_rgb->h;
	dst.w = m_overlay->w;
	dst.h = m_overlay->h;
	SDL_BlitOverlay(m_yuv, &src, m_overlay, &dst);

/*
	if( m_yuv )
	{
		SDL_Rect offset;
		offset.x = 0;
		offset.y = 0;
		offset.w = m_video_width;
		offset.h = m_video_height;

		// int SDL_BlitSurface(SDL_Surface *src, SDL_Rect *srcrect, SDL_Surface *dst, SDL_Rect *dstrect);
		SDL_BlitSurface( surface, NULL, m_screen, &offset );

		SDL_Rect srcoff;

		static SDL_YUVSurface *yuv = SDL_CreateYUVSurface(m_screen->w, m_screen->h);

		//Get_YUV_From_Surface(surface, yuv);
		ConvertRGBtoYUV(m_screen, yuv);

		srcoff.x = 0;
		srcoff.y = 0;
		srcoff.w = yuv->w;
		srcoff.h = yuv->h;

		SDL_Rect overoff;
		overoff.x = 0;
		overoff.y = 0;
		overoff.w = m_overlay->w;
		overoff.h = m_overlay->h;

		SDL_BlitOverlay(yuv, &srcoff, m_overlay, &overoff);
		//SDL_BlitOverlay(m_yuv_surface, &srcoff, m_overlay, &offset);
	}
*/

}

void	CVideo::Present()
{
	SDL_Flip( m_screen );
}
